/**
 *  *2010-6-9 上午12:32:46  add 
 */
package com.base.myproject.client.ui;

import com.base.myproject.client.busi.print.Printer;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.tools.Tools;
import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Widget;

/**
 * 调用jsp页面，查询专用
 * 
 * @author Administrator 2010-6-9 javazgw@gmail.com
 */
public class BaseOpenJSP extends BaseJDBCPanel implements Printer,
		Listener<BaseEvent> {
	static int NoDuplicateNum = 0;// 不重复数
	// com.extjs.gxt.ui.client.widget.FramePanel frame =
	// com.extjs.gxt.ui.client.widget.FramePanel
	// .pop();

	Frame frame = new Frame();
	public String id = "";
	Button print = new Button("打印");
	String jspfile = "";

	public BaseOpenJSP(String jspfile) {

		if (jspfile.indexOf("?") > 0) {
			this.jspfile = jspfile + "&r=" + Random.nextInt()+"&CURBCODE="+Env.getEnvbcode();
		} else {
			this.jspfile = jspfile + "?r=" + Random.nextInt()+"&CURBCODE="+Env.getEnvbcode();
		}
		NoDuplicateNum++;
		id = NoDuplicateNum + "";

		init();

	}

	public void init() {
		initWeigetx();
	}


	private Widget initWeigetx() {
		setLayout(new RowLayout(Orientation.VERTICAL));

		add(frame, new RowData(1, 1, new Margins(10)));
	//	add(print);

		//frame.getElement().setId(id);

		frame.getElement().setAttribute("frameborder", "1");
		frame.setUrl(jspfile);

		this.setHeight(400);
		Tools.setResize(frame, 1, 1);
		Tools.setResize(this, 1, 1);
		return null;

	}

	public void Print() {

		if (GXT.isIE) {
			printFrame_IE("0");
		} else {
			printFrame("0");
		}
	}

	/**
	 * ie下的frame打印
	 * 
	 */

	static native void printFrame_IE(String id)/*-{
		$wnd.frames[id].focus();
		$wnd.frames[id].print();
	}-*/;

	/**
	 * 非ie下的打印
	 */
	static native void printFrame(String id) /*-{
		var iframe = $doc.frames ? $doc.frames[id] : $doc.getElementById(id);
		var ifWin = iframe.contentWindow || iframe;
		ifWin.print();
	}-*/;

	static native void tt(String id) /*-{
		$wnd.alert(id);
	}-*/;

	public String getId() {

		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Override
	public void handleEvent(BaseEvent be) {
		if (be.getType().equals(Events.Select))

			if (be.getSource().equals(print)) {
				Print();
				// frame.el().setElementAttribute("src", "busijsp/test.jsp");
			}

	}

	public Frame getFrame() {
		return frame;
	}

	public void setFrame(Frame frame) {
		this.frame = frame;
	}

	public String getJspfile() {
		return jspfile;
	}

	public void setJspfile(String jspfile) {
		this.jspfile = jspfile;
	}

}
